<?php
class Tools {

// Преобразования
    public static function utf2ansi($s) { return $s ? iconv('UTF-8','CP1251',$s) : ''; }
    public static function ansi2utf($s) { return $s ? iconv('CP1251','UTF-8',$s) : ''; }

// Проверки
    public static function is_boolstr( $s ) {
        $s = strtolower($s);
        return is_string( $s ) && ( $s == 'true' || $s == 'false' );
    }
    
    public static function is_numeric_array( $arr ) {
        $res = true;
        if( !count($arr) ) return false;
        foreach( $arr as $value ) {
            if( !is_numeric($value) ) { $res = false; break; }
        }
        return $res;
    }

    public static function check_referer() {
        return preg_match("|^http(s)?://".$_SERVER['SERVER_NAME']."/|", $_SERVER['HTTP_REFERER']) ? true : false;
    }
    
// Ajax & json
    public static function check_ajax_referer() {
        return ( preg_match("|^http(s)?://".$_SERVER['SERVER_NAME']."/|", $_SERVER['HTTP_REFERER']) &&
                isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 
                strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ) ? true : false;
    }
    public static function json_response( $errmess, $errcode=0, $data = null ) {
        $json_arr = array( 
                'errcode'=>$errcode, 
                'errmess'=>$errmess,
                'data'=>$data 
                );
        header("Content-type: text/script;charset=utf-8");
        echo json_encode( $json_arr );   
    }
}
?>
